gusucode.com > VC++ 界面很酷的一款多媒体播放器 > VC++ 界面很酷的一款多媒体播放器/gusucode/xMedia代码注释中/maincontrol.cpp

    //Download by http://www.NewXing.com
/****************************************
*	File:			maincontrol.cpp		*
*	Last modified:	2004.2.7			*
*	Author:			Yuanyi-Zhang		*
*	Version:		0.0.0.2				*
*	CopyLeft 2004   xMedia team			*
****************************************/
/****************************************************************
*																*
* This file may be distributed and/or modified under the terms	*
* of the GNU General Public License version 2 as published by	*
* the Free Software Foundation and appearing in the file		*
* LICENSE.GPL included in the packaging of this file.			*
*																*
****************************************************************/

#pragma warning (disable:4786)

#include <windows.h>
#include <commctrl.h>
#include <tchar.h>

#include "messagedef.h"
#include "maincontrol.h"
#include "cover.h"

#define IDM_VIDEOWND		1000
#define IDM_PLAYLIST		1001
#define IDM_MINIBROW		1002
#define IDM_EQ				1003
#define IDM_ABOUT			1004
#define IDM_QUIT			1005

/////////////////////////
//设置播放进度
void MainControl::setTime()
{
	int playProg;

	playProg = m_lpPlayer->getPlayProg();
	
	m_lpMainWnd->setPlayProg( playProg );

	long length;

	length = m_lpPlayer->getLength();

	m_lpMainWnd->setLength( length );

	long bps = m_lpPlayer->getBPS();

	m_lpMainWnd->setBPS( bps );

	m_lpMainWnd->setTitle( m_lpPlayList->getCurFilePath() );

	bool audioOnly;

	audioOnly = m_lpPlayer->getAudioOnly();

	if( !audioOnly )
	{
		m_lpVideoWnd->setPlayProg( playProg );
	}
}
//////////////////////////
//创建各个模块的用户交互界面
bool	MainControl::Create( HINSTANCE hInstance )
{
	Cover c ( "cover.bmp" );
	c.Display ( hInstance );
	INITCOMMONCONTROLSEX iccex; 
	string strSkinFileName;
	string strSkinName;

	//初始化通用控件
    iccex.dwICC = ICC_WIN95_CLASSES;
    iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
	
	if( !InitCommonControlsEx(&iccex) )
	{
		m_ErrorString = _T("初始化通用控件库失败!");
		goto fail;
	}

	m_lpILConfig=new IniLoad;
	m_lpSkinConfig=new IniLoad;

	if( !m_lpILConfig || !m_lpSkinConfig )
	{
		m_ErrorString = _T("内存不足!");
		goto fail;
	}

	if( !m_lpILConfig->Load( _T("xMedia.ini") ))
	{
		m_ErrorString = _T("找不到指定文件xMedia.ini!");
		goto fail;
	}
	
	strSkinName = m_lpILConfig->Get( _T("skin") );
	
	if( strSkinName.compare( _T( "" ) ) == 0 )
	{
		m_ErrorString = _T("配置文件已被损坏!");
		goto fail;
	}

	strSkinFileName = _T( "skin\\" ) + strSkinName  
		+ _T( ".skin" );


	if( !m_lpSkinConfig->Load( strSkinFileName ) )
	{
		m_ErrorString = _T( "找不到指定文件!" );
		goto fail;
	}

	m_lpMainWnd->SetIniLoad( m_lpSkinConfig );
	m_lpEQ->SetIniLoad( m_lpSkinConfig );
	m_lpPlayList->SetIniLoad( m_lpSkinConfig );
	m_lpMiniBrow->SetIniLoad( m_lpSkinConfig );
	m_lpVideoWnd->SetIniLoad( m_lpSkinConfig );

	if( !m_lpMainWnd->Create( hInstance ) )
	{
		m_ErrorString = m_lpMainWnd->GetErrorString();
		goto fail;
	}

	if( !m_lpEQ->Create( hInstance, m_lpMainWnd->m_hWnd ) )
	{
		m_ErrorString = m_lpEQ->GetErrorString();
		goto fail;
	}

	if( !m_lpPlayList->Create( hInstance, m_lpMainWnd->m_hWnd ) )
	{
		m_ErrorString = m_lpPlayList->GetErrorString();
		goto fail;
	}

	if( !m_lpMiniBrow->Create( hInstance, m_lpMainWnd->m_hWnd ) )
	{
		m_ErrorString = m_lpMiniBrow->GetErrorString();
		goto fail;
	}
	
	if( !m_lpVideoWnd->Create( hInstance, m_lpMainWnd->m_hWnd ) )
	{
		m_ErrorString = m_lpVideoWnd->GetErrorString();
		goto fail;
	}

	m_lpPlayer->setVideoWnd( m_lpVideoWnd->m_hWnd, m_lpVideoWnd->m_VideoRect );

	m_ErrorString = _T( "" );
	
	c.Hide();
	return true;
fail:		//如果失败则释放资源并退出
	c.Hide();
	m_lpMainWnd->ReleaseResource();
	m_lpEQ->ReleaseResource();
	m_lpPlayList->ReleaseResource();
	m_lpMiniBrow->ReleaseResource();
	m_lpVideoWnd->ReleaseResource();
	ReleaseResource();
	return false;
}
//////////////////////////////
//消息循环
int	MainControl::MsgProc()
{
	MSG Msg;
	while( GetMessage( &Msg, NULL, 0, 0 ) )
	{	
		if( Msg.hwnd != NULL )
		{
			TranslateMessage( &Msg );
			DispatchMessage( &Msg );
		}
		else
		{
			switch( Msg.message )
			{
			case XM_RELEASERESOURCE:		//释放资源
				m_lpMainWnd->ReleaseResource();
				m_lpEQ->ReleaseResource();
				m_lpPlayList->ReleaseResource();
				m_lpMiniBrow->ReleaseResource();
				m_lpVideoWnd->ReleaseResource();
				m_lpPlayer->ReleaseResource();
				ReleaseResource();
				break;
			case MC_SETTIME:				//设置时间
				setTime();
				break;
			case MC_SEEKING:				//定位
				if( !seeking() )
				{
					MessageBox( NULL, m_ErrorString.c_str(), _T( "Error" ), 
						MB_OK|MB_ICONWARNING );
				}
				break;
			case MC_POPUPMENU:				//弹出右键菜单
				popupMenu();
				break;
			case MC_PLAY:					//开始播放
				if( !Play() )
				{
					MessageBox( NULL, m_ErrorString.c_str(), _T( "Error" ), 
						MB_OK|MB_ICONWARNING );
				}
				break;
			case MC_STOP:					//停止播放 
				if( !Stop() )
				{
					MessageBox( NULL, m_ErrorString.c_str(), _T( "Error" ), 
						MB_OK|MB_ICONWARNING );
				}
				break;
			case MC_PAUSE:					//暂停播放
				if( !Pause() )
				{
					MessageBox( NULL, m_ErrorString.c_str(), _T( "Error" ), 
						MB_OK|MB_ICONWARNING );
				}
				break;
			case MC_PLAYNEXT:				//播放下一首
				if( !PlayNext() )
				{
					MessageBox( NULL, m_ErrorString.c_str(), _T( "Error" ), 
						MB_OK|MB_ICONWARNING );
				}
				break;
			case MC_PLAYPREV:				//播放上一首
				if( !PlayPrev() )
				{
					MessageBox( NULL, m_ErrorString.c_str(), _T( "Error" ), 
						MB_OK|MB_ICONWARNING );
				}
				break;
			case XM_VWBUTTON:				//显示/隐藏视频窗口
				m_bShowVideoWnd = m_lpVideoWnd->Show();
				break;
			case XM_EQBUTTON:				//显示/隐藏EQ窗口
				m_bShowEQ = m_lpEQ->Show();
				break;
			case XM_PLAYLIST:				//显示/隐藏播放列表窗口
				m_bShowPlayList = m_lpPlayList->Show();
				break;
			case XM_MBBUTTON:				//显示/隐藏迷你浏览器窗口
				m_bShowMiniBrow = m_lpMiniBrow->Show();
				break;
			default:
				SendMessage( m_lpMainWnd->m_hWnd, Msg.message, Msg.wParam, Msg.lParam );
				SendMessage( m_lpEQ->m_hWnd, Msg.message, Msg.wParam, Msg.lParam );
				SendMessage( m_lpPlayList->m_hWnd, Msg.message, Msg.wParam, Msg.lParam );
				SendMessage( m_lpMiniBrow->m_hWnd, Msg.message, Msg.wParam, Msg.lParam );
				SendMessage( m_lpVideoWnd->m_hWnd, Msg.message, Msg.wParam, Msg.lParam );
			}
		}
	}

	return Msg.wParam;
}
//////////////////////////////////
//释放资源
void MainControl::ReleaseResource()
{
	if( m_lpMainWnd != NULL )
	{
		delete m_lpMainWnd;
	}
	if( m_lpEQ != NULL )
	{
		delete m_lpEQ;
	}
	
	if( m_lpPlayList != NULL )
	{
		delete m_lpPlayList;
	}
	
	if( m_lpMiniBrow != NULL )
	{
		delete m_lpMiniBrow;
	}
	
	if( m_lpVideoWnd != NULL )
	{
		delete m_lpVideoWnd;
	}
	
	if( m_lpPlayer != NULL )
	{
		delete m_lpPlayer;
	}

	if( m_lpILConfig != NULL )
	{
		delete m_lpILConfig;
	}

	if( m_lpSkinConfig != NULL )
	{
		delete m_lpSkinConfig;
	}
}

/////////////////////////////
//播放曲目
bool MainControl::Play()
{
	string strFilePath = m_lpPlayList->getCurFilePath();

	if( strFilePath.compare( _T("") ) == 0 )
	{
		return true;
	}
	if( !m_lpPlayer->Play( strFilePath ) )
	{
		m_ErrorString = m_lpPlayer->GetErrorString();
		return false;
	}
	return true;
}
////////////////////////////
//停止播放
bool MainControl::Stop()
{
	if( !m_lpPlayer->Stop() )
	{
		m_ErrorString = m_lpPlayer->GetErrorString();
		return false;
	}
	return true;
}
///////////////////////////
//暂停播放
bool MainControl::Pause()
{
	if( !m_lpPlayer->Pause() )
	{
		m_ErrorString = m_lpPlayer->GetErrorString();
		return false;
	}
	return true;
}
//////////////////////////
//播放下一首
bool MainControl::PlayNext()
{
	m_lpPlayList->gotoNext();
	return Play();
}
//////////////////////////
//播放上一首
bool MainControl::PlayPrev()
{
	m_lpPlayList->gotoPrev();
	m_lpPlayer->Close();
	return Play();
}
//////////////////////////
//定位
bool MainControl::seeking()
{
	int prog = m_lpMainWnd->getPlayProg();

	if( prog != -1 )
	{
		m_lpPlayer->seeking( prog );
	}

	prog = m_lpVideoWnd->getPlayProg();

	if( prog != -1 )
	{
		m_lpPlayer->seeking( prog );
	}
	
	m_lpMainWnd->setProgComplete();
	return true;
}
/////////////////////////////
//弹出右键菜单
void MainControl::popupMenu()
{
	HMENU hMenu;

	hMenu = CreatePopupMenu();

	InsertMenu( hMenu, -1, MF_BYCOMMAND,
			IDM_VIDEOWND, _T("视频窗口") );
	InsertMenu( hMenu, -1, MF_BYCOMMAND|MF_BYPOSITION|MF_STRING,
			IDM_PLAYLIST, _T("播放列表") );
	InsertMenu( hMenu, -1, MF_BYCOMMAND|MF_BYPOSITION|MF_STRING,
			IDM_EQ, _T("均衡器") );
	InsertMenu( hMenu, -1, MF_BYCOMMAND|MF_BYPOSITION|MF_STRING,
			IDM_MINIBROW, _T("迷你浏览器") );
	InsertMenu( hMenu, -1, MF_SEPARATOR|MF_BYPOSITION,
			0, NULL);
	InsertMenu( hMenu, -1, MF_BYCOMMAND|MF_BYPOSITION|MF_STRING,
			IDM_ABOUT, _T("关于xMedia Player...") );
	InsertMenu( hMenu, -1, MF_BYCOMMAND|MF_BYPOSITION|MF_STRING,
			IDM_QUIT, _T("退出") );

	if( m_bShowVideoWnd )
	{
		CheckMenuItem( hMenu, IDM_VIDEOWND, MF_CHECKED );
	}

	if( m_bShowPlayList )
	{
		CheckMenuItem( hMenu, IDM_PLAYLIST, MF_CHECKED );
	}

	if( m_bShowMiniBrow )
	{
		CheckMenuItem( hMenu, IDM_MINIBROW, MF_CHECKED );
	}

	if( m_bShowEQ )
	{
		CheckMenuItem( hMenu, IDM_EQ, MF_CHECKED );
	}

	POINT p;

	GetCursorPos( &p );
	TrackPopupMenu( hMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON, p.x, p.y, 
			0, m_lpMainWnd->m_hWnd, NULL );

	DeleteObject( hMenu );
}